var $=KISSY.all;
KISSY.use("node",function(S,Node){ 
function __dialog(title,addon,top,width){
			var tstr = top?"top:"+top+"px;":"";
			var wstr = width?"width:"+width+"px;":"width:830px";
			var dialog =("<div class=\"bui-dialog\" style=\""+wstr+"visibility:visible;display:block;"+tstr+"\">");
			dialog+=("    <div class=\"bui-stdmod-header\">");
			dialog+=("        <div class=\"header-title\">");
			dialog+=("            "+title+"");
			dialog+=("        </div>");
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-body\">");
			dialog+=("    "+addon);
			dialog+=("    </div>");
			dialog+=("    <div class=\"bui-stdmod-footer\">");
			dialog+=("        <button class=\"button button-primary cancel\">");
			dialog+=("            关闭");
			dialog+=("        </button>");
			dialog+=("    </div>");
			dialog+=("    <a class=\"bui-ext-close\">");
			dialog+=("        <span class=\"bui-ext-close-x x-icon x-icon-normal\">");
			dialog+=("            ×");
			dialog+=("        </span>");
			dialog+=("    </a>");
			dialog+=("</div>");
			return dialog;
}
function showDialog(title,addon,show){
	var top=($("."+show).outerHeight())/2;
	var width=($("."+show).innerWidth());
	// var mask = ("<div style=\"width:100%;left:0;top:0;height:100%;position:fixed;\" class=\"bui-ext-mask\"></div>");
	// $(mask).insertBefore("."+show);
	$(__dialog(title,addon,top,width)).insertBefore("."+show);
	$(".bui-ext-close-x").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
	$(".cancel").on("click",function(){$(".bui-ext-mask").remove();$(".bui-dialog").remove();$(".doc-content").removeClass("height240");});	
	// $(".bui-dialog").scrollIntoView();
}
function closeDialog(){
$(".bui-ext-close-x").fire("click");
}
$(".js-suoqu").on("click",function(){
	var fuid = $(this).attr("data-fuid");
	suoqu(fuid);
});
$(".js-zengsong").on("click",function(){
	var fuid = $(this).attr("data-fuid");
	suoqu(fuid,"zengsong");
});
$(".qiandao").on("click",function(){
		KISSY.io({
		url: "/?a=qiandao&m=front&c=index",
		success: function(data, textStatus) {
			if(data.login==false){
				showDialog("尚未登录","您尚未登录，请登录后继续！<br />","buyers");
				$(".bui-stdmod-body").css("text-align","center");
				$(".J_TB_Login").clone(true,true,true).appendTo(".bui-stdmod-body").removeClass("hide");
			}else{
				alert(data.msg);
			}
		},
		complete: function(data){
			
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
});
// $(".buyer").on("mouseout",function(){
	// $(".refresh").hide();
// });
$(".buyer").on("mouseenter",function(){
	$(".refresh").show();
});
var usrpg=0;
$(".js-showicon").on("mouseenter mouseout",function(e){
	var togid = $(this).attr("data-id");
	if(e.type=="mouseenter"){
	$("."+togid).removeClass("hide");
	}else{
	$("."+togid).addClass("hide");
	}
	
});
$(".refresh").on("click",function(){
	KISSY.io({
		url: "/?a=rfrusers&m=front&c=event",
		data:{"pg":usrpg},
		success: function(data, textStatus) {
			if(data.status==false){
				alert("已经没有更多用户了！");
				return;
			}
			var usr="";
			for(var i=0;i<data.length;i++){
				usr+=("<div class=\"buyer\">");
				usr+=("        <div class=\"avatar\">");
				usr+=("                    <p><img src=\""+data[i].avatar+"\"></p>");
				usr+=("                    <a class=\"suo js-suoqu\" data-fuid=\""+data[i].id+"\"><img src=\"http://img04.taobaocdn.com/imgextra/i4/27390555/TB2ouFfaXXXXXbYXpXXXXXXXXXX_!!27390555.jpg\"></a>");
				usr+=("                </div>");
				usr+=("                <div class=\"stat\">");
				usr+=("                    <p>抽奖次数：<span>"+data[i].lotts+"次</span></p>");
				usr+=("                    <p>中奖次数：<span>"+data[i].wins+"次</span></p>");
				usr+=("                    <p>中奖率：<span>"+data[i].val+"/100</span></p>");
				usr+=("                    ");
				usr+=("                    <a href=\"/?m=front&c=event#\"><img src=\"http://img04.taobaocdn.com/imgextra/i4/27390555/TB2vuhfaXXXXXX4XpXXXXXXXXXX_!!27390555.jpg\" style=\"border:0px;\"></a>");
				usr+=("                </div>");
				usr+=("    </div>");
			}
			$(".buyer").remove();
			$(usr).appendTo(".buyers");
			$(".js-suoqu").on("click",function(){
				var fuid = $(this).attr("data-fuid");
				suoqu(fuid);
			});
			usrpg++;
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
});
function suoqu(fuid,action){
if(!action){
	action="suoqu";
}
KISSY.io({
	url: "/?a="+action+"&m=front&c=index",
	data: {"fuid":fuid},
	success: function(data, textStatus) {
		closeDialog();
		if(data.login==false){
			showDialog("尚未登录","您尚未登录，请登录后继续！<br />","buyers");
			$(".bui-stdmod-body").css("text-align","center");
			$(".J_TB_Login").clone(true,true,true).appendTo(".bui-stdmod-body").removeClass("hide");
		}else{
			alert(data.msg);
		}
	},
	complete: function(data){
		
	},
	error: function(){
		alert('error');
	},
	dataType:"jsonp"
});
}
$(".action").on("click",function(){
	var eid = $(this).attr("data-eid");
	KISSY.io({
		url: "/?a=lottery&m=front&c=event",
		data: {"eid":eid},
		success: function(data, textStatus) {
		closeDialog();
		var giftname = data.giftname;
		var gid = data.gid;
			if(data.status==false){
				showDialog("再接再厉",data.giftname+"<br />","event");
				$(".bui-stdmod-body").css("text-align","center");
				if(data.login==false){
					$(".J_TB_Login").clone(true,true,true).appendTo(".bui-stdmod-body").removeClass("hide");
				}
			}else if(data.status=="mission"){
				showDialog("请完成任务","<div>"+data.giftname+"</div>","event");
				var mission = data.mission.split(',');
				for (var i=0;i<mission.length;i++){
					$(".sns-"+mission[i]).clone(true,true,true).appendTo(".bui-stdmod-body");
				}
			}else{
				if(data.type==1){
				var html = "<div style='text-align:center'>恭喜您中奖"+data.giftname+"！请领取中奖凭证，向活动组织人出示：<div><img src='"+data.giftpic+"_160x160.jpg' /></div><a href='/?c=Certi&m=front&gid="+data.ugid+"' target='_blank' class=\"btn_s\">领取中奖凭证</a></div>"
					showDialog("恭喜您中奖！",html,"event");
				return;
				//以下是输入领奖信息，直接领奖的方法
					var truename = "";
					var province = "";
					var city = "";
					var district = "";
					var address = "";
					var mobile = "";
					KISSY.io({
						url: "/?a=getaddr&m=front&c=index",
						success: function(data, textStatus) {
							truename = data.truename==null?"":data.truename;
							province = data.province==null?"":data.province;
							city = data.city==null?"":data.city;
							district = data.district==null?"":data.district;
							address = data.address==null?"":data.address;
							mobile = data.mobile==null?"":data.mobile;
							$(".kg_province").val(province);
							$(".kg_province2").empty();
							$(".kg_province3").empty();
							$(".kg_province2").on("change",function(){if(this.value==0){$(".kg_province").val("");$(this).empty();}});
							$(".kg_province3").on("change",function(){if(this.value==0){$(".kg_province").val("");$(".kg_province2").empty();$(this).empty();}});
							if(data.city!=null){
							$("<option value=\"0\">重新选择</option>").appendTo(".kg_province2");
							$("<option value=\"0\">重新选择</option>").appendTo(".kg_province3");
							$("<option value=\""+data.city+"\">"+data.cit_name+"</option>").appendTo(".kg_province2");
							$("<option value=\""+data.district+"\">"+data.dis_name+"</option>").appendTo(".kg_province3");
							}
							// $(".kg_province2").val(city);
							// $(".kg_province3").val(district);
							var html = "<div style='text-align:center'>恭喜您中奖"+giftname+"！";
								html += "请补全领奖信息：<div class='inputuinfo'>";
								html += "<div><p>收奖人姓名：</p><input type='text' name='truename' class='truename' value='"+truename+"'/></div> ";
								html += "<div><p>收奖地址：</p><div class=\"area\"></div></div>";
								html += "<div><p>街道：</p><input type='text' name='address' class='address' value='"+address+"'/></div>";
								html += "<div><p>联系电话：</p><input type='text' name='mobile' class='mobile' value='"+mobile+"'/> </div>";
								html += "<div style=\"text-align:center\"><button class=\"saving\">保存并催发奖</button></div>";
								html += "</div></div>"
								showDialog("恭喜您中奖！",html,"event");
								$(".saving").on("click",function(){
									var truename = $(".truename").val();
									var province = $(".kg_province").val();
									var city = $(".kg_province2").val();
									var district = $(".kg_province3").val();
									var address = $(".address").val();
									var mobile = $(".mobile").val();
									var msg="";
									if(truename==""){
										msg="\n真实姓名不能为空！";
									}
									if(province==""){
										msg+="\n请选择省份！";
									}
									if(city==""){
										msg+="\n请选择城市！";
									}
									if(district==""){
										msg+="\n请选择市区！";
									}
									if(address==""){
										msg+="\n请填写街道地址！";
									}
									if(mobile==""||mobile.length!=11){
										msg+="\n请填写有效的收货电话！";
									}
									if(msg!=""){
										alert(msg);
										return;
									}
										KISSY.io({
											url: "/?a=saveaddr&m=front&c=index",
											data: {"truename":truename,
												   "province":province,
												   "city":city,
												   "district":district,
												   "address":address,
												   "mobile":mobile},
											success: function(data, textStatus) {
											$(".select_x").addClass("select").appendTo(".event");
											closeDialog();
												if(data.status==1){
													showDialog("成功！","<div style='text-align:center'>已顺利保存领奖信息，请问是否查看所有中奖信息？<br /><button class=\"seeall\">是</button><button class=\"close\">否</button></div>","event");
													$(".seeall").on("click",function(){location.href=("/?a=gifts&m=user")});
													$(".close").on("click",function(){closeDialog()});
												}else{
												showDialog("出错","完成任务出错。","event");
												}
											},
											complete: function(data){
												
											},
											error: function(){
												alert('error');
											},
											dataType:"jsonp"
										});
								});
								$(".select").removeClass("select").addClass("select_x").appendTo(".area");
								KISSY.Event.detach('.cancel');
								$(".cancel").on("click",function(){$(".select_x").addClass("select").appendTo(".event");closeDialog();});
								$(".bui-ext-close").addClass("hide");
							},
						complete: function(data){
							
						},
						error: function(){
							alert('error');
						},
						dataType:"jsonp"
					});
				}else{
					var html = "<div style='text-align:center'>恭喜您中奖"+data.giftname+"！点击图片领奖：<br /><a href='"+data.withdraw+"' target='_blank' class='ljcon'><img src='"+data.giftpic+"_160x160.jpg' /></a></div>"
					showDialog("恭喜您中奖！",html,"event");
					$(".ljcon").on("click",function(){
							KISSY.io({
								url: "/?a=ljconfirm&m=user",
								data: {"gid":gid},
								success: function(data, textStatus) {
									closeDialog();
									if(data.status==1){
									$(".status"+gid).empty();
									$("<span class=\"label label-warning\">已领奖</span>").appendTo(".status"+gid);
										showDialog("请领奖！","请在新打开的窗口中领奖！所有中奖均可在“<a href=\"/?a=gifts&m=user\">我的奖品</a>”中查看。","event");
									}else{
										showDialog("已领过！","已领过该奖品！","event");
									}
								},
								complete: function(data){
								},
								error: function(){
									alert('error');
								},
								dataType:"jsonp"
							});
					});
				}
				
			}
		},
		complete: function(data){
			
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
});
$(".sns-like").on("click",function(){
mission("likeme",$(this).attr("data-eid"));
});
$(".sns-followshop").on("click",function(){
mission("collect",$(this).attr("data-eid"));
});
$(".sns-friendfollow").on("click",function(){
mission("followme",$(this).attr("data-eid"));
});
$(".sns-sharebtn").on("click",function(){
mission("shareme",$(this).attr("data-eid"));
});

function mission(mis,eid){
	KISSY.io({
		url: "/?a=mission&m=front&c=event",
		data: {"mis":mis,"eid":eid},
		success: function(data, textStatus) {
		closeDialog();
			if(data.status==false){
				showDialog("出错","完成任务出错。","event");
			}else{
				//showDialog("成功！","恭喜您完成任务！","event");
			}
		},
		complete: function(data){
			
		},
		error: function(){
			alert('error');
		},
		dataType:"jsonp"
	});
}
var anim = new KISSY.LayerAnim(
    {

        node: '.gifts',
        form: {
            scrollTop: 0
        },
        to: {
            scrollTop: 100
        },
		duration: 5,
		yoyo:true,
		repeatDelay:5,
        repeat: -1
    });
anim.run();
});